<?php
// +----------------------------------------------------------------------
// |收藏
// +----------------------------------------------------------------------
// |Data:2016-06-27 下午4:08
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Wap\Controller;
use Common\Controller\CommonController;

class CollectionController extends CommonController{
    private $_model;
    protected function _initialize(){
        parent::_initialize();
        $this->_model = D('Wap/Collection');
    }

    /**
     * 收藏列表
     */
    public function index(){
        $type = I('get.type',0);//1评论最多 2距离最近 3销售量 4起送价
        $map = $this->search();

        $ord = 'c.add_time desc';
        if($type == 3){
            $ord = 'sales_nums desc';
        }else if($type == 4){
            $ord = 'min_send_price desc';
        }else if($type == 1){
            $ord = 'comment_num desc';
        }
        //$arr = array('8497.32','8110.74');
        $list = $this->_model->getCollectionList($map,$ord);
        foreach($list as $key=>&$val){
            list($s_time,$e_time) = explode(',',$val['sales_time']);
            $val['s_time'] = $s_time;
            $val['e_time'] = $e_time;
            $devilerArr = unserialize($val['deviler_price']);
            asort($devilerArr['sprice']);
            $val['deviler_price'] = $devilerArr['sprice'][0];
            if($val['free_price']){
                $freeArr = unserialize($val['free_price']);
                asort($freeArr['dprice']);
                $val['free_price'] = $freeArr['dprice'][0];
            }else{
                $val['free_price'] = 0;
            }
            $val['pic'] = showImage($val['pic'],'shop',$val['uid'])[0];
            $val['score'] = D('Wap/Comment')->getShopScore($val['id']);
            list($lng,$lat) = explode(',',$val['location']);
            $val['distance'] = getDistance($this->location,$lat.','.$lng);
            //$val['distance'] = $arr[$key];
        }

        if($type == 2){
            $list =  $this->distance($list);
        }
        $this->assign('list',$list);
        $this->assign('type',$type);
        $this->display();
    }

    /**
     * 构造搜索条件
     * @return array
     */
    private function search(){
        $cid = I('get.cid',0);
        $status = I('get.status',0);//1营业中 2可预定

        $map = array();
        $map['c.uid'] = $this->uid;
        !empty($cid) && $map['cate_id'] = $cid;
        if($status == 1){
            $map['status'] = 1;
        }else if($status == 2){
            $map['is_reservation'] = 1;
        }

        $cateList = D('Admin/ShopCate')->getList();
        $this->assign('cateList',$cateList);
        $this->assign('cid',$cid);
        $this->assign('status',$status);

        return $map;
    }

    /**
     * 距离最近
     * @param $data
     * @return bool
     */
    private function distance($data){
        if(empty($data)) return false;
        foreach($data as $key=>$val){
            $distance[$key] = $val['distance'];
        }
        array_multisort($distance, SORT_ASC, $data);
        return $data;
    }

    /**
     * 收藏、取消操作
     */
    public function collection(){
        $shop_id = I('get.id');
        empty($shop_id) && $this->returnData('请选择操作项!');
        $isHas = $this->_model->isHas($shop_id,$this->uid);
        $type = $isHas ? 2 : 1;
        $res = $this->_model->doCollection($shop_id,$this->uid,$type);
        $msg = $type == 1 ? '收藏' : '取消收藏';
        if($res){
            $this->returnData($msg.'成功!',1);
        }else{
            $this->returnData($msg.'失败!');
        }
    }
}
?>